package edu.yuhf.web.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.yuhf.dao.iface.UserDao;
import edu.yuhf.entity.User;

@Controller
@RequestMapping("/lar")
public class LoginAndRegisterController {
	
	@Resource
	UserDao userDao;

	@RequestMapping("/login1")
	public String login(@RequestParam(value="id") String id,@RequestParam(value="name") String name,boolean flag){
		System.out.println("@RequestParam："+name+","+id);
		return "login";
	}
	
	@RequestMapping("/login/{id}/{name}")
	public String login(@PathVariable(value="id") String id,@PathVariable(value="name") String name){
		System.out.println("REST ful模式获取的数据："+name+","+id);
		return "login";
	}
	
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		ModelAndView modeAndView=new ModelAndView();
		Map<String,Object> model=modeAndView.getModel();
		User user=userDao.checkUserNameAndPassword(userName, password);
		if(null!=user){
			request.getSession().setAttribute("user", user);
			modeAndView.setViewName("views/main");
		}else{
			model.put("message", "用户名或密码错误！");
			modeAndView.setViewName("login");
		}
		return modeAndView;
	}
}
